<template>
	<view class="load" v-if="!load">
		<u-loading-icon mode="circle" inactive-color="#fff" text="正在加载..." vertical></u-loading-icon>
	</view>
	<block v-else>
		<view class="container">
			<view class="head">
				<view class="searchBox">
					<image src="https://giftapp.oss-cn-shenzhen.aliyuncs.com/upload/1721975652195245.png" mode="">
					</image>
					<input type="text" v-model="searchValue" @input="searchCustomers" placeholder="请输入客户名称/电话"/>
					<!-- <button style="width: 50rpx; height: 30rpx; background-color: #000;"></button> -->
				</view>
			</view>
			<view style="height: 120rpx;"></view>
			<block v-if="customList.length > 0">
				<view class="listBox">
					<block v-for="(item, index) in customList" :key="index">
						<view class="item">
							<view class="company" style="display: none;">
								<image src="https://giftapp.oss-cn-shenzhen.aliyuncs.com/upload/1721975656651459.png"
									mode="">
								</image>
								<view class="company_name">
									{{ item.Company }}
								</view>
							</view>

							<view class="border">
								<view class="customer">
									<view class="left">
										<view class="customer_name">
											{{ item.TrueName }}
										</view>
										<view class="customer_position" v-if="item.Tag">
											{{ item.Tag }}
										</view>
									</view>

									<view class="edit" @click="setAddress(item)">
										编辑
									</view>
								</view>

								<view class="phone">
									<image
										src="https://giftapp.oss-cn-shenzhen.aliyuncs.com/upload/17219756605381404.png"
										mode="">
									</image>
									<view class="tel">
										{{ item.Phone }}
									</view>
								</view>

								<view class="phone position">
									<image
										src="https://giftapp.oss-cn-shenzhen.aliyuncs.com/upload/17219756639191392.png"
										mode="">
									</image>
									<view class="tel">
										{{ item.Province }}{{ item.City }}
										<span v-if="item.Area">{{ item.Area }}</span>
										{{ item.Address }}
									</view>
								</view>
							</view>

						</view>
					</block>
				</view>
			</block>

			<block v-else>
				<EmptyBox image="https://giftapp.oss-cn-shenzhen.aliyuncs.com/upload/1721977846162417.png"
					text="还没客户信息哦~">
				</EmptyBox>
			</block>
		</view>
		<view style="height: 220rpx;"></view>
		<view class="saveBox">
			<view class="btn addBtn" @click="nav_to('/pages/custom/addCustom')">
				<image src="https://giftapp.oss-cn-shenzhen.aliyuncs.com/upload/1721975666899758.png" mode="widthFix">
				</image>
				添加客户
			</view>
			<view class="tips" @click="nav_to('/pages/custom/import?type=1')">
				批量导入
			</view>
		</view>
	</block>
</template>

<script>
	export default {
		data() {
			return {
				load: false,
				customList: [],
				isMore: true, //是否还有更多
				PageIndex: 1, //页码
				PageSize: 10,
				searchValue: '', // 搜索的输入值
			};
		},

		computed: {},
		watch: {

		},

		onReady() {

		},
		async onLoad(e) {
			uni.$on('customUpdate',()=>{
				this.fetchData();
			})
		},

		async onShow() {
			this.fetchData();
		},
		onUnload() {
			uni.$off('customUpdate')
		},

		methods: {

			// 获取客户列表
			async fetchData(params = {}) {
				const res = await this.$api.post('/Customer/PageList', {
					PageIndex: this.PageIndex, 	// 页码
					PageSize: this.PageSize,	// 一页数量
					...params
				});
				if (res.code == 0) {
					this.customList = this.PageIndex == 1 ? this.customList = res.data : this.customList.concat(res.data)
					
					this.load = true;
				}

				this.isMore = Array.isArray(res.data) && res.data.length >= this.PageSize;
			},
			
			searchCustomers (e) {
				this.$util.debounce(()=>{
					this.PageIndex = 1; // 重置页码
					this.searchValue = e.detail.value.replace(/\s+/g, "")
					this.fetchData({ trueName: this.searchValue });
				},300)
				
			},
			
			// 编辑地址
			setAddress(item) {
				this.$store.commit("setAddressMsg", item);

				this.nav_to('/pages/custom/editCustom')
			},
		},
		onReachBottom() {
			if (this.isMore) {
				this.PageIndex++;
				this.fetchData();
			}
		},
	};
</script>

<style lang="scss">
	page {
		// height: 100%;
		background: #f6f6f6;
	}

	.container {
		display: flex;
		flex-direction: column;
		align-items: center;

		.head {
			width: 100%;
			height: 98rpx;
			background: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;
			position: fixed;
			top: 0;
			left: 0;
			box-shadow: 0 0 8rpx #ccc;
			.searchBox {
				width: 93%;
				height: 72rpx;
				background: #F6F6F6;
				border-radius: 36rpx;
				display: flex;
				align-items: center;
				justify-content: flex-start;
				font-size: 28rpx;
				color: #666666;

				image {
					width: 32rpx;
					height: 32rpx;
					margin: 0 10rpx 0 20rpx;
				}
				
				input {
					width: 80%;
				}
			}
		}

		.listBox {
			width: 100%;
			padding: 0 24rpx;
			padding-bottom: 40rpx;
		}

		.item {
			display: flex;
			flex-direction: column;
			background-color: #FFFFFF;
			border-radius: 16rpx;
			margin-top: 15rpx;
			padding: 0 25rpx;
			width: 100%;

			.company {
				display: flex;
				align-items: center;
				height: 88rpx;
				border-bottom: 1rpx solid #ECECEC;
				// opacity: 0.5;

				image {
					width: 40rpx;
					height: 40rpx;
					margin-right: 5rpx;
				}

				.company_name {
					font-weight: bold;
					font-size: 28rpx;
					color: #333333;
				}
			}

			.border {
				border-bottom: 1rpx solid #ECECEC;
				padding: 26rpx 0;

				.customer {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.left {
						display: flex;
						// justify-content: space-between;
						align-items: center;

						.customer_name {
							font-weight: bold;
							font-size: 32rpx;
							color: #333333;
						}

						.customer_position {
							font-size: 20rpx;
							color: #5d8bf8;
							background: #FFF3F0;
							border-radius: 4rpx;
							height: 32rpx;
							line-height: 32rpx;
							margin-left: 15rpx;
							padding: 0 8rpx;
						}
					}

					.edit {
						font-size: 24rpx;
						color: #333333;
						width: 96rpx;
						height: 48rpx;
						background: #F6F6F6;
						border-radius: 24rpx;
						display: flex;
						justify-content: center;
						align-items: center;
					}
				}

				.phone {
					display: flex;
					margin: 10rpx 0;

					image {
						width: 32rpx;
						height: 32rpx;
						margin: 5rpx 8rpx 0 0rpx;
					}

					.tel {
						font-size: 28rpx;
						color: #666666;
						width: 87%;
						display: flex;
					}
				}
			}

			.border:last-child {
				border-bottom: none;
			}
		}


	}

	.saveBox {
		position: fixed;
		width: 100%;
		box-sizing: border-box;
		left: 0;
		bottom: 0;
		padding: 20rpx 50rpx 60rpx;
		background-color: #fff;
		// display: flex;
		// justify-content: center;
		.tips{
			color: #333;
			text-align: center;
			text-decoration: underline;
			margin-top: 10rpx;
		}
		.addBtn {
			width: 100%;
			height: 88rpx;
			border-radius: 44rpx;
			font-weight: bold;
			font-size: 32rpx;
			color: #5d8bf8;
			background-color: #fff;
			border: 2rpx solid #5d8bf8;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 32rpx;
				height: 32rpx;
				margin-right: 8rpx;
			}
		}
	}
</style>